package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompThirdMchntEntryService;
import cc.rengu.igas.bomp.facade.request.*;
import cc.rengu.igas.bomp.facade.response.*;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompThirdMchntEntryServiceImpl implements BompThirdMchntEntryService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<InstMchntRegisterResponse> instMchntRegister(InstMchntRegisterRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<InstMchntRegisterResponse> result = new Result<>();
        InstMchntRegisterResponse response = new InstMchntRegisterResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, InstMchntRegisterResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (InstMchntRegisterResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("第三方机构商户进件处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WechatRealNameApplyResponse> wechatRealNameApply(WechatRealNameApplyRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRealNameApplyResponse> result = new Result<>();
        WechatRealNameApplyResponse response = new WechatRealNameApplyResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatRealNameApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WechatRealNameApplyResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信商户实名认证申请处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WechatRealNameApplyCancelResponse> wechatRealNameApplyCancel(WechatRealNameApplyCancelRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRealNameApplyCancelResponse> result = new Result<>();
        WechatRealNameApplyCancelResponse response = new WechatRealNameApplyCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatRealNameApplyCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WechatRealNameApplyCancelResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信商户实名认证申请撤销处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WechatRealNameAuditQueryResponse> wechatRealNameAuditQuery(WechatRealNameAuditQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRealNameAuditQueryResponse> result = new Result<>();
        WechatRealNameAuditQueryResponse response = new WechatRealNameAuditQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatRealNameAuditQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WechatRealNameAuditQueryResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信实名认证申请审核结果查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WechatRiskMchntQueryResponse> wechatRiskMchntQuery(WechatRiskMchntQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRiskMchntQueryResponse> result = new Result<>();
        WechatRiskMchntQueryResponse response = new WechatRiskMchntQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatRiskMchntQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatRiskMchntQueryResponse) dubboResult.getResult());
            response = (WechatRiskMchntQueryResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信风险商户违规查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<InstMchntQueryResponse> instMchntQuery(InstMchntQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<InstMchntQueryResponse> result = new Result<>();
        InstMchntQueryResponse response = new InstMchntQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, InstMchntQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (InstMchntQueryResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("第三方机构商户入驻查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WechatActivityMchntListQueryResponse> wechatActivityMchntListQuery(WechatActivityMchntListQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatActivityMchntListQueryResponse> result = new Result<>();
        WechatActivityMchntListQueryResponse response = new WechatActivityMchntListQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WechatActivityMchntListQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WechatActivityMchntListQueryResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信活动费率商户列表查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WeChatActivityRateRegistResponse> weChatActivityRateRegist(WeChatActivityRateRegistRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WeChatActivityRateRegistResponse> result = new Result<>();
        WeChatActivityRateRegistResponse response = new WeChatActivityRateRegistResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WeChatActivityRateRegistResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WeChatActivityRateRegistResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信活动费率申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WeChatActivityRateRegistQueryResponse> weChatActivityRateRegistQuery(WeChatActivityRateRegistQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WeChatActivityRateRegistQueryResponse> result = new Result<>();
        WeChatActivityRateRegistQueryResponse response = new WeChatActivityRateRegistQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WeChatActivityRateRegistQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WeChatActivityRateRegistQueryResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信活动费率申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<WeChatActivityRateModifyResponse> weChatActivityRateModify(WeChatActivityRateModifyRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WeChatActivityRateModifyResponse> result = new Result<>();
        WeChatActivityRateModifyResponse response = new WeChatActivityRateModifyResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, WeChatActivityRateModifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (WeChatActivityRateModifyResponse) dubboResult.getResult();
            result.setResult(response);
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("微信活动费率申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }
}
